//
//  UITextField+ZSLimit.m
//  ZSLimitTextField-master
//
//  Created by safiri on 2018/7/2.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "UITextField+ZSLimit.h"
#import <objc/runtime.h>

@implementation UITextField (ZSLimit)

//MARK: - getters and setters

- (NSUInteger)maxLength {
    NSNumber *number = objc_getAssociatedObject(self, _cmd);
    return number.integerValue;
}
- (void)setMaxLength:(NSUInteger)maxLength {
    objc_setAssociatedObject(self, @selector(maxLength), @(maxLength), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)characterLimitString {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setCharacterLimitString:(NSString *)characterLimitString {
    objc_setAssociatedObject(self, @selector(characterLimitString), characterLimitString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setCharacterLimitSet:[NSCharacterSet characterSetWithCharactersInString:characterLimitString]];
}

- (NSCharacterSet *)characterLimitSet {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setCharacterLimitSet:(NSCharacterSet *)characterLimitSet {
    objc_setAssociatedObject(self, @selector(characterLimitSet), characterLimitSet, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)canInputSpaceCharacter {
    NSNumber *number = objc_getAssociatedObject(self, _cmd);
    if (number) {
        return number.boolValue;
    }
    return NO;
}
- (void)setCanInputSpaceCharacter:(BOOL)canInputSpaceCharacter {
    objc_setAssociatedObject(self, @selector(canInputSpaceCharacter), [NSNumber numberWithBool:canInputSpaceCharacter], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)isUppercaseString {
    NSNumber *number = objc_getAssociatedObject(self, _cmd);
    if (number) {
        return number.boolValue;
    }
    return NO;
}
- (void)setIsUppercaseString:(BOOL)isUppercaseString {
    objc_setAssociatedObject(self, @selector(isUppercaseString), [NSNumber numberWithBool:isUppercaseString], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UITextFieldZSLimitType)limitType {
    NSNumber *number = objc_getAssociatedObject(self, _cmd);
    return number.integerValue;
}
- (void)setLimitType:(UITextFieldZSLimitType)limitType {
    if (limitType == LimitIdCardType) {
        self.characterLimitString = IdCardCharacterSetString;
        self.maxLength = 18;
        self.isUppercaseString = YES;
    }else if (limitType == LimitNumberType) {
        self.characterLimitString = NumberCharacterSetString;
    }else if (limitType == LimitPhoneType) {
        self.characterLimitString = NumberCharacterSetString;
        self.maxLength = 11;
    }else if (limitType == LimitPhoneSpaceType) {
        self.characterLimitString = NumberCharacterSetString;
        self.maxLength = 11;
    }else if (limitType == LimitNumberEnglishAlphabet) {
        NSMutableCharacterSet *characterSet = [NSMutableCharacterSet lowercaseLetterCharacterSet];
        [characterSet formUnionWithCharacterSet:[NSCharacterSet uppercaseLetterCharacterSet]];
        [characterSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
        self.characterLimitSet = characterSet;
    }
    objc_setAssociatedObject(self, @selector(limitType), [NSNumber numberWithInteger:limitType], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)limitWithValueChangeString:(NSString *)string shouldChangeCharactersInRange:(NSRange)range {
    
    // 是否可以输入空格
    if (!self.canInputSpaceCharacter) {
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    }
    
    // 是否转大写控制
    if (self.isUppercaseString) {
        string = [string uppercaseString];
    }
    
    // 输入字符控制
    if (self.characterLimitSet) {
        if ([string rangeOfCharacterFromSet:[self.characterLimitSet invertedSet]].location != NSNotFound) {
            return NO;
        }
    }
    
    NSString *text = self.text;
    text = [text stringByReplacingCharactersInRange:range withString:string];
    //text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    // 长度控制
    if (self.maxLength > 0) {
        text = [text substringToIndex:MIN(text.length, self.maxLength)];
    }
    
    [self setText:text];
    
    return NO;
}

- (BOOL)limitForPhoneSpaceTypeWithValueChangeString:(NSString *)string shouldChangeCharactersInRange:(NSRange)range {
    
    NSString *text = self.text;
    
    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
        return NO;
    }
    //DLog(@"%@",NSStringFromRange(range));
    text = [text stringByReplacingCharactersInRange:range withString:string];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSString *newString = @"";
    
    NSString *subString = [text substringToIndex:MIN(text.length, 3)];
    
    newString = [newString stringByAppendingString:subString];
    if (subString.length == 3) {
        newString = [newString stringByAppendingString:@" "];
    }
    
    text = [text substringFromIndex:MIN(text.length, 3)];
    
    if (text.length > 0) {
        NSString *subString2 = [text substringToIndex:MIN(text.length, 4)];
        newString = [newString stringByAppendingString:subString2];
        if (subString2.length == 4) {
            newString = [newString stringByAppendingString:@" "];
            
        }
        NSString *subString3 = [text substringFromIndex:MIN(text.length, 4)];
        newString = [newString stringByAppendingString:subString3];
        
    }
    
    newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
    
    
    if (newString.length >= 14) {
        return NO;
    }
    
    [self setText:newString];
    
    return NO;
}
@end
